<template>
  <div class="pageWrapper">
    <common-header />
    <div class="content">
      <div class="card">
        <div class="title">{{ submitForm.year }}年度报告</div>
      </div>
      <div class="card des-wrapper">
        <div class="des-title">年度报告内容</div>
        <textarea id="content" @click="onTextArea" v-model="submitForm.reportContent" placeholder="请输入年度报告内容"></textarea>
        <div class="sub-title">附件<span>（最多可传5个）</span></div>
        <uploadFile ref="upload" @fileChange="fileChange" />
      </div>
      <div class="btn-group">
        <div class="btn btn-active" @click="onSubmit(1)">保存</div>
        <div class="btn btn-active" @click="onSubmit(2)">提交</div>
      </div>
    </div>
  </div>
</template>

<script>
import uploadFile from "@/components/uploadFile";
import { saveAnnualReport, getAddReportDetail } from "@/api/annualReport";
import { Toast } from "vant";
export default {
  components: {
    uploadFile
  },
  data() {
    return {
      submitForm: {
        deptId: "",
        year: "",
        reportContent: "",
        status: "",
        fileSaveDTOList: []
      },
      start: "",
      end: ""
    };
  },
  created() {
    let params = this.$route.query;
    if (params.id) {
      this.submitForm.id = params.id;
      this.initData();
    } else {
      this.submitForm.year = +params.year;
      this.submitForm.deptId = +params.deptId;
    }
  },
  methods: {
    onTextArea() {
      this.$nextTick(() => {
        document.querySelector('#content').focus();
      })
    },
    initData() {
      getAddReportDetail(this.submitForm.id).then(res => {
        this.submitForm = res.data;
        this.submitForm.fileSaveDTOList = res.data.fileVOList;
        this.$refs.upload.setData(
          res.data.fileVOList.map(ele => {
            return {
              name: ele.fileName,
              size: ele.fileSize,
              url: ele.fileUrl,
              fileId: ele.fileId
            };
          })
        );
      });
    },
    fileChange(file) {
      this.submitForm.fileSaveDTOList = file.map(ele => {
        return {
          fileName: ele.name,
          fileSize: ele.size,
          fileUrl: ele.url
        };
      });
    },
    onSubmit(val) {
      if (!this.submitForm.reportContent) {
        Toast("请输入记实内容");
        return;
      }
      this.submitForm.status = val;
      saveAnnualReport(this.submitForm).then(res => {
        if (res.code == 200) {
          Toast(this.submitForm.dataId ? "编辑成功" : "添加成功");
          this.$router.back();
        } else {
          Toast(res.msg);
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.content {
  padding: 20px 16px;

  .card {
    padding: 16px;
    background: #fff;
    border-radius: 8px;

    .title {
      line-height: 20px;
      color: #333;
      font-size: 14px;
    }

    .time {
      line-height: 17px;
      color: #a3a7ad;
      font-size: 12px;
      margin-top: 3px;
    }
  }
}

.des-wrapper {
  margin-top: 12px;

  .des-title {
    padding-top: 4px;
    font-size: 16px;
    color: #000;
    line-height: 22px;
  }

  textarea {
    width: 100%;
    padding: 0;
    color: #333;
    line-height: 20px;
    margin-top: 16px;
    height: 136px;
    resize: none;
    border: none;
    font-size: 14px;

    &::placeholder {
      color: #b2b2b2;
    }
  }
}

.sub-title {
  position: relative;
  margin-top: 16px;
  margin-bottom: 12px;
  color: #333333;
  padding-left: 12px;
  font-size: 14px;

  span {
    color: #cccccc;
  }

  &::before {
    content: "";
    position: absolute;
    width: 4px;
    height: 14px;
    top: 2px;
    left: 0;
    border-radius: 1px;
    background: #367fff;
  }
}

.btn-group {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 35px 0;

  .btn {
    width: 164px;
    text-align: center;
    line-height: 54px;
    border-radius: 12px;
    background: #2a81e6;
    color: #fff;
    font-size: 14px;

    &:first-child {
      color: #2a81e6;
      background: #fff;
    }
  }
}</style>
